package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SearchResultListAdapter;
import com.ihighest.app.infox.tools.CheckNetWork;

/**
 * 搜索详情
 * 
 * @author Vity Created at 2015-11-2 上午3:10:24
 * @version 1.0
 */
public class SearchResultActivity extends BaseActivity {
	static final String TAG = "SearchActivity";
	PullToRefreshListView mRefreshListView;
	SearchResultListAdapter adapter;
	TextView tvNoResult;

	EditText etKeyword;
	JSONArray array;

	int page = 1;
	long time = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_search_result);
		initView();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void initView() {
		tvNoResult = (TextView) findViewById(R.id.noresult);
		adapter = new SearchResultListAdapter(SearchResultActivity.this);
		mRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefresh);
		etKeyword = (EditText) findViewById(R.id.etKeyword);
		etKeyword.setText(getIntent().getStringExtra("key"));

		findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		mRefreshListView.setAdapter(adapter);
		mRefreshListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					arg2 = arg2 - 1;
					JSONObject json = array.getJSONObject(arg2);
					String type = json.getString("type");
					Intent intent = null;
					if ("1".equalsIgnoreCase(type)) {
						intent = new Intent(SearchResultActivity.this, InformationalContentActivity.class);
						intent.putExtra("newsId", json.getString("id"));
					} else if ("2".equalsIgnoreCase(type)) {
						intent = new Intent(SearchResultActivity.this, InformationalContentActivity.class);
						intent.putExtra("newsId", json.getString("id"));
					} else if ("3".equalsIgnoreCase(type)) {
						intent = new Intent(SearchResultActivity.this, InformationalContentActivity.class);
						intent.putExtra("newsId", json.getString("id"));
					} else if ("4".equalsIgnoreCase(type)) {
						intent = new Intent(SearchResultActivity.this, QuestionContentActivity.class);
						intent.putExtra("id", json.getString("id"));
						intent.putExtra("title", json.getString("title"));
						intent.putExtra("image", json.getString("image"));
						intent.putExtra("time", json.getLong("time"));
					} else if ("5".equalsIgnoreCase(type)) {
						intent = new Intent(SearchResultActivity.this, BusinessOpportunityActivity.class);
						intent.putExtra("id", json.getString("id"));
					}
					startActivity(intent);
				} catch (Exception e) {
					Log.e(TAG, e.getMessage(), e);
				}

			}
		});
		mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				if (CheckNetWork.isConnected(SearchResultActivity.this)) {
					page = 1;
					time = -1;
					load();
				} else {
					ToastKit.showShort(SearchResultActivity.this, "请连接网络");
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				if (CheckNetWork.isConnected(SearchResultActivity.this)) {
					page++;
					load();
				} else {
					ToastKit.showShort(SearchResultActivity.this, "请连接网络");
				}
			}

		});
		if (CheckNetWork.isConnected(SearchResultActivity.this)) {
			page = 1;
			time = -1;
			load();
		} else {
			ToastKit.showShort(SearchResultActivity.this, "请连接网络");
		}
	}

	void load() {
		ApiParam param = ApiParam.create();
		param.addParam("key", etKeyword.getEditableText().toString());
		param.addParam("page", page);
		if (time >= 0) {
			param.addParam("time", time);
		}

		Api.get("search", param, new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					if (page == 1) {
						tvNoResult.setVisibility(View.VISIBLE);
						mRefreshListView.setVisibility(View.GONE);
					} else {
						tvNoResult.setVisibility(View.GONE);
						mRefreshListView.setVisibility(View.VISIBLE);
						page--;
					}
				} else {
					time = result.getTime();
					tvNoResult.setVisibility(View.GONE);
					mRefreshListView.setVisibility(View.VISIBLE);
					if (page > 1) {
						array = adapter.append(result.getDatas());
					} else {
						array = adapter.set(result.getDatas());
					}
					adapter.notifyDataSetChanged();
				}
				mRefreshListView.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				page--;
			}
		}, this);
	}
}
